<template>
	<view class="container">
		<view class="flexBox">
			<uni-search-bar style="background-color: #fff;flex: 1;" @confirm="getShopInfo" v-model="keyword"
				placeholder="请输入手机号/商铺名称" @clear=""></uni-search-bar>
			<view @click="scanCode" style="background-color: #fff;line-height: 50px;padding-right:10px;">
				<uni-icons type="scan" color="#5cabfd" size="30"></uni-icons>
			</view>
		</view>
		<view class=" " v-if="starShow">
			<view class="merchant" v-show="entity.Title" style="">
				<view class="flexBox">
					<uni-icons type="shop"></uni-icons>
					<view>
						{{entity.Title || ""}}
					</view>
				</view>
				<view class="flexBox">
					<uni-icons type="location"></uni-icons>
					<view>
						{{entity.Address || ""}}
					</view>
				</view>
				<view class="flexBox alignItemCenter">
					<uni-icons type="phone"></uni-icons>
					<view class="flexBox alignItemCenter between flex1">
						<view class="flex1">
							{{entity.Phone || ""}}
						</view>
						<view class="flexBox">
							<view class="star" @click="star">
								<uni-icons class="lineIcon" type="star" size="20" color="#e88f21"></uni-icons>
								收藏
							</view>
							<!-- <view class="iconBox" @click="AgreementPage">
								<uni-icons class="lineIcon" type="cart-filled" color="#5cabfd" size="22"></uni-icons>
								去赊货
							</view> -->
						</view>
					</view>
				</view>
			</view>
		</view>
		<view  v-if="data.length>0">
			<view class="merchant">
				我收藏的卖家
				<view class="merchantItem" v-for="item in data" :key="item.Id" @click="AgreementPage(item)">
					<view class="flexBox">
						<uni-icons type="shop"></uni-icons>
						<view>
							{{item.Title}}
						</view>
					</view>
					<view class="flexBox">
						<uni-icons type="location"></uni-icons>
						<view>
							{{item.Address}}
						</view>
					</view>
					<view class="flexBox alignItemCenter">
						<uni-icons type="phone"></uni-icons>
						<view class="flexBox alignItemCenter between flex1">
							<view class="flex1">
								{{item.Phone}}
							</view>
							<view class="star" @click.stop="canceStar(item)">
								<uni-icons class="lineIcon" type="star-filled" size="20" color="#e88f21"></uni-icons>
								取消收藏
							</view>
							<view class="iconBox">
								<uni-icons class="lineIcon" color="#5cabfd" type="cart-filled" size="22"></uni-icons>
								<text>去赊货</text>
							</view>
						</view>
					</view>
				</view>
			</view>
			<view class="" style="color:#aaa;text-align: center;padding-top:50rpx">
				没有更多了
			</view>
		</view>
		
		<view v-else style="color: #aaa;
    text-align: center;
    margin-top: 30px;">
			<!-- 上拉显示更多 -->
			<view class="" style="padding-bottom: 100rpx;">
				<uniLoadMore :status="Status" :contentText="contentText" :rows="data" :otherTitle="entity.Title"></uniLoadMore>
			</view>
		</view>
	</view>
</template>

<script>
	// 引入加载更多
	import uniLoadMore from '@/components/uni-load-more.vue'
	export default {
		components: {
			uniLoadMore
		},
		data() {
			return {
				// 加载状态
				Status: "loading",
				contentText: {
					contentdown: '',
					contentrefresh: '加载中',
					contentnomore: '没有更多了'
				},
				entity: {},
				data: [],
				keyword: '',
				pagations: {
					"PageIndex": 1,
					"PageRows": 25,
					"SortField": "Id",
					"SortType": "desc",
				},
				starShow: true
			}
		},
		onLoad() {
			this.userInfo = uni.getStorageSync('userInfo')
		},
		onShow() {
			// 获取收藏的公司列表 数据
			this.getDataList()
		},
		// watch: {
		// 	$route(to, from) {
		// 			console.log('直接通过', to,from)
		// 			if(from.path === '/pages/GoodsPage/GoodsPage'){
		// 				this.$store.commit('clearCarList')
		// 			}
		// 	}
		// },
		methods: {
			AgreementPage(item) {
				console.log('tiaotiaotiao', item)
				if(item.BankId==null){
					// 没有银行
					uni.showToast({
						title:'没有银行，暂时无法赊货',
						duration:1500,
						icon:"none"
					})
					return
				}
				uni.setStorageSync('shopInfo', item)
				uni.navigateTo({
					url: '/pages/AgreementPage/AgreementPage?obj=' +JSON.stringify(item)
				})
			},
			// 取消收藏
			canceStar(e) {
				this.$http('post', '/Base_Manage/Base_CompanyCollect/DeleteData', [e.CollectId]).then(res => {
					if (res.Success) {
						this.$api.msg('操作成功')
						this.getDataList()
					} else this.$api.msg(res.Msg)
				})
			},
			// 收藏
			star() {
				this.$http('post', '/Base_Manage/Base_Company/AddCollect', {
					// "BuyerStoreId": "string",
					"CompanyId": this.entity.Id,
					"CreatorId": this.userInfo.Id ? this.userInfo.Id : '15201611813'
				}).then(res => {
					console.log(res);
					if (res.Success) {
						this.starShow = false
						this.$api.msg('收藏商铺成功')
						this.getDataList()
					} else this.$api.msg(res.Msg)
				})
			},
			// 获取商家详细信息
			getShopInfo(e) {
				this.starShow = true
				this.$http('post', '/Base_Manage/Base_Company/GetEnterprise', {
					"keywords": e.value
				}).then(res => {
					console.log('获取商家详细信息', res, this.entity)
					if (res.Success && res.Data) {
						this.entity = res.Data
					} else this.$api.msg('查询商铺不存在')
				})
			},
			getDataList() {
				this.Status = "loading"
				this.$http('post', '/Base_Manage/Base_Company/GetCompanyEnterpriseList', {
					...this.pagations,
					"Search": {
						"CreatorId": this.userInfo.Id
					}
				}).then(res => {
					uni.hideLoading()
					this.data = res.Data
					this.entity.Title = res.Data.Title
					this.Status = "noMore"
				})
			},
			scanCode() {
				// 允许从相机和相册扫码
				uni.scanCode({
					onlyFromCamera: true,
					success: (res)=> {
						console.log('条码类型：' + res.scanType);
						console.log('条码内容：' + res.result);
						if (res.scanType !== 'QR_CODE') return uni.showToast({
							icon: 'none',
							title: '请扫描二维码'
						})
						let arr = res.result.split(',')
						if (arr.length < 2 || arr[0] != '01') return uni.showToast({
							icon: 'none',
							title: '请扫描正确的二维码'
						})
						this.starShow = true
						this.$http('post', '/Base_Manage/Base_Company/GetEnterprise', {
							"keywords": arr[1]
						}).then(res => {
							if (res.Success && res.Data) {
								this.entity = res.Data
							} else this.$api.msg('查询商铺不存在')
						})
					}
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.flex1 {
		flex: 1;
	}

	.flexBox {
		display: flex;
	}

	.alignItemCenter {
		align-items: center;
	}

	.between {
		justify-content: space-between;
	}

	.container {

		.merchant {
			background-color: #fff;
			border-radius: 10px;
			box-shadow: 1px 1px 5px #ccc;
			box-sizing: border-box;
			padding: 20rpx;
			margin: 10px;
			color: #666;

			.uni-icons {
				margin-right: 10px;
				vertical-align: bottom;
			}

			.lineIcon {
				margin-right: 0;
			}

			.iconBox {
				color: #5cabfd;
			}

			.star {
				margin-right: 10px;
				color: #e88f21;
			}

			.merchantItem {
				padding-bottom: 5px;
				margin-bottom: 10px;
				border-bottom: 1px solid #ccc;


			}
		}
	}
</style>